#include <iostream>
#include "windows.h"
#include "processthreadsapi.h"

BOOL CALLBACK DebugEventCallback(DWORD dwDebugEventCode, LPDEBUG_EVENT lpDebugEvent)
{
    switch (dwDebugEventCode)
    {
    case EXCEPTION_DEBUG_EVENT:
        break;
    case CREATE_THREAD_DEBUG_EVENT:
        break;
    case EXIT_THREAD_DEBUG_EVENT:
        break;
    case LOAD_DLL_DEBUG_EVENT:
        break;
    case UNLOAD_DLL_DEBUG_EVENT: 
        break;
    case OUTPUT_DEBUG_STRING_EVENT: 
        break;
    case RIP_EVENT:
        break;
    case CREATE_PROCESS_DEBUG_EVENT:
        // 在子进程创建时可以进行一些初始化操作
        break;
    case EXIT_PROCESS_DEBUG_EVENT:
        // 子进程退出时的处理
        return FALSE;
    }
    return TRUE;
}
int main(int args,char** argv)
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));
    if (!CreateProcess(NULL, argv[1], NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &si, &pi))
    {
        std::cerr << "Failed to start target process for debugging." << std::endl;
        return 1;
    }

    DEBUG_EVENT debugEvent;
    DWORD continueStatus = DBG_CONTINUE;
    while (WaitForDebugEvent(&debugEvent, INFINITE))
    {
        if (!DebugEventCallback(debugEvent.dwDebugEventCode, &debugEvent))
            break;

        ContinueDebugEvent(debugEvent.dwProcessId, debugEvent.dwThreadId, continueStatus);
    }

    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);

    return 0;
}